package com.hellobike.haxing.modules.supplier.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hellobike.haxing.common.annotation.AutoLog;
import com.hellobike.haxing.common.system.base.controller.BaseController;
import com.hellobike.haxing.common.system.query.QueryGenerator;
import com.hellobike.haxing.common.vo.Result;
import com.hellobike.haxing.modules.address.util.Convert2KV;
import com.hellobike.haxing.modules.purchase.service.ITPurchaseOrderDetailService;
import com.hellobike.haxing.modules.purchase.service.ITPurchaseProService;
import com.hellobike.haxing.modules.supplier.entity.TSupplierClPro;
import com.hellobike.haxing.modules.supplier.entity.TSupplierClProRecord;
import com.hellobike.haxing.modules.supplier.service.ITSupplierClProRecordService;
import com.hellobike.haxing.modules.supplier.service.ITSupplierClProService;
import com.hellobike.haxing.modules.supplier.util.BeanUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.TreeSet;
import java.util.stream.Collectors;

import static java.util.Comparator.comparing;
import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.toCollection;

/**
 * @Description: 供应商商品表
 * @Author: haxing-boot
 * @Date:   2020-01-15
 * @Version: V1.0
 */
@RestController
@RequestMapping("/supplier/tSupplierClPro")
@Slf4j
public class TSupplierClProController extends BaseController<TSupplierClPro, ITSupplierClProService> {
	@Autowired
	private ITSupplierClProService tSupplierClProService;

	@Autowired
	private ITSupplierClProRecordService itSupplierClProRecordService;

	@Autowired
	private ITPurchaseOrderDetailService itPurchaseOrderDetailService;

	@Autowired
	private ITPurchaseProService itPurchaseProService;

	/**
	 * 分页列表查询
	 *
	 * @param tSupplierClPro
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "供应商商品表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(TSupplierClPro tSupplierClPro,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<TSupplierClPro> queryWrapper = QueryGenerator.initQueryWrapper(tSupplierClPro, req.getParameterMap());
		Page<TSupplierClPro> page = new Page<TSupplierClPro>(pageNo, pageSize);
		IPage<TSupplierClPro> pageList = tSupplierClProService.page(page, queryWrapper);
		return Result.ok(pageList);
	}

	/**
	 * 通过id生成供应商信息
	 *
	 * @param json
	 * @return
	 */
	@PostMapping(value = "/getBySupplierId")
	public Result<?> getBySupplierId(@RequestBody JSONObject json) {
		Long headerId = json.getLong("headerId");
		return Result.ok(tSupplierClProService.getBySupplierId(headerId));
	}

	/**
	 * 通过id生成供应商信息
	 *
	 * @param json
	 * @return
	 */
	@PostMapping(value = "/getSupplier4Price")
	public Result<?> getSupplier4Price(@RequestBody JSONObject json) {
		QueryWrapper<TSupplierClPro> queryWrapper = new QueryWrapper<>();
		Long headerId = json.getLong("headerId");
		return Result.ok(tSupplierClProService.getById(headerId));
	}

	/**
	 * 通过id查询
	 *
	 * @param json
	 * @return
	 */
	@PostMapping(value = "/initSuppliersBySku")
	public Result<?> initSuppliersBySku(@RequestBody JSONObject json) {
		Long headerId = json.getLong("headerId");
		QueryWrapper<TSupplierClPro> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("sku_id", headerId);
		List<TSupplierClPro> tSupplierClPros = tSupplierClProService.list(queryWrapper);
		if(tSupplierClPros==null || tSupplierClPros.size() < 1) {
			return Result.error("未找到对应数据");
		}
		List<TSupplierClPro> res = tSupplierClPros.stream().collect(collectingAndThen(
				toCollection(() -> new TreeSet<>(comparing(n->n.getId()))), ArrayList::new)
		);
		return Result.ok(Convert2KV.SupplierInfo2kv(res));
	}


	/**
	 *   添加
	 *
	 * @param tSupplierClPro
	 * @return
	 */
	@AutoLog(value = "供应商商品表-添加",operateType=2)
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody TSupplierClPro tSupplierClPro) {
		tSupplierClProService.save(tSupplierClPro);
		return Result.ok("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param tSupplierClPro
	 * @return
	 */
	@AutoLog(value = "供应商商品表-编辑",operateType=3)
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody TSupplierClPro tSupplierClPro) {
		tSupplierClProService.updateById(tSupplierClPro);
		return Result.ok("编辑成功!");
	}

	/**
	 *  编辑
	 *
	 * @return
	 */
	@AutoLog(value = "供应商商品表-编辑",operateType=3)
	@PutMapping(value = "/edit4Zhe")
	public Result<?> edit4Zhe() {
		return Result.ok("编辑成功!");
	}

	/**
	 *  编辑
	 *
	 * @param tSupplierClPro
	 * @return
	 */
	@AutoLog(value = "供应商商品记录表-编辑",operateType=3)
	@PutMapping(value = "/editBySupplier")
	public Result<?> editBySupplier(@RequestBody TSupplierClPro tSupplierClPro) {
		TSupplierClProRecord tSupplierClProRecord = new TSupplierClProRecord();
		BeanUtils.copyProperties(tSupplierClPro,tSupplierClProRecord);
		itSupplierClProRecordService.save(tSupplierClProRecord);
		tSupplierClProService.updateById(tSupplierClPro);
//		 更新 供应商未确认的商品的供应量
//		itPurchaseOrderDetailService.updateBySkuIdAndSupplier(tSupplierClPro.getSkuId(),tSupplierClPro.getSupplyVolume());
		// 同步供应价到pro表
//		itPurchaseProService.updatePurchaseProFromClp(tSupplierClPro.getSkuId(),tSupplierClPro.getSupplierPrice());
		return Result.ok("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "供应商商品表-删除",operateType=4)
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) Long id) {
		tSupplierClProService.removeById(id);
		return Result.ok("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "供应商商品表-批量删除",operateType=4)
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.tSupplierClProService.removeByIds(Arrays.asList(ids.split(",")).stream().map(s -> Long.parseLong(s.trim())).collect(Collectors.toList()));
		return Result.ok("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) Long id) {
		TSupplierClPro tSupplierClPro = tSupplierClProService.getById(id);
		if(tSupplierClPro==null) {
			return Result.error("未找到对应数据");
		}
		return Result.ok(tSupplierClPro);
	}

	/**
	 * 导出excel
	 *
	 * @param request
	 * @param tSupplierClPro
	 */
	@AutoLog(value = "供应商商品表-导出excel",operateType=6)
	@RequestMapping(value = "/exportXls")
	public ModelAndView exportXls(HttpServletRequest request, TSupplierClPro tSupplierClPro) {
		return super.exportXls(request, tSupplierClPro, TSupplierClPro.class, "供应商商品表");
	}

	/**
	 * 通过excel导入数据
	 *
	 * @param request
	 * @param response
	 * @return
	 */
	@AutoLog(value = "供应商商品表-导入excel",operateType=5)
	@RequestMapping(value = "/importExcel", method = RequestMethod.POST)
	public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
		return super.importExcel(request, response, TSupplierClPro.class);
	}

}
